<template>
  <a-modal v-model:open="open"
           width="50%"
           :footer="null"
           style="top:100px"
           :title="formState.id?'修改船员证书':'添加船员证书'"
           :keyboard="false"
           :mask-closable="false"
           @cancel="handleClose">
    <a-form
        :model="formState"
        name="basic"
        class="mt20"
        layout="horizontal"
        style="width: 100%;"
        ref="formRef"
        :rules="rules"
        :label-col=" { style: { width: '150px' } }"
        :wrapper-col="{ span: 24}"
        autocomplete="off"
    >
      <a-row class="flex justify-center">
        <a-col :span="18">
          <a-form-item label="培训项目代码" name="xiangMuDaiMa">
            <a-input v-model:value.trim="formState.xiangMuDaiMa" maxlength="100" placeholder="请填写"/>
          </a-form-item>

          <a-form-item label="培训项目名称" name="xiangMuMing">
            <a-input v-model:value.trim="formState.xiangMuMing" maxlength="100" placeholder="请填写"/>
          </a-form-item>

          <a-form-item label="有效期（单位:月）" name="youXiaoQi">
            <a-input-number v-model:value.trim="formState.youXiaoQi" maxlength="5" style="width:300px;" placeholder="请填写"/>
          </a-form-item>

          <a-form-item label="预警天数（单位:天）" name="yuJingTianShu">
            <a-input-number v-model:value.trim="formState.yuJingTianShu" maxlength="5" style="width:300px;" placeholder="请填写"/>
          </a-form-item>

          <!--<a-form-item
              label="签发日期"
              name="qianFaRiQi">
            <a-date-picker v-model:value.trim="formState.qianFaRiQi" placeholder="请填写" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </a-form-item>
          <a-form-item
              label="有效期"
              name="youXiaoQi">
            <a-date-picker v-model:value.trim="formState.youXiaoQi" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="请填写"/>
          </a-form-item>-->

          <a-form-item
              label="状态"
              name="zhuangTai">
            <a-switch v-model:checked="formState.zhuangTai"
                      :checked-value="1"
                      :un-checked-value="0"
                      checked-children="激活"
                      un-checked-children="未激活"/>
          </a-form-item>
          <a-form-item>
            <a-button style="margin-left: 150px" type="primary" @click.stop.prevent="onSubmit">
              <EditOutlined v-if="formState.id "></EditOutlined>
              <PlusOutlined v-else></PlusOutlined>
              {{ formState.id ? '更新船员证书' : '保存船员证书' }}
            </a-button>
            <a-button style="margin-left: 10px" danger type="primary" @click.stop.prevent="handleClose">
              <close-outlined></close-outlined>
              关闭
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script setup>
const open = ref(false);
const formRef = ref({})
const emits = defineEmits(["success"])
const userStore = useStore.userState()
const formState = ref({
  xiangMuDaiMa: '',
  xiangMuMing: '',
  youXiaoQi: '',
  yuJingTianShu: '',
  zhuangTai: 1
});

// 验证规则
const rules = {
  xiangMuDaiMa: [{required: true, message: '请填写培训项目代码', trigger: 'blur'}],
  xiangMuMing: [{required: true, message: '请输入培训项目名称', trigger: 'blur'}],
  /*youXiaoQi: [{required: true, message: '请输入有效期', trigger: 'blur'}],*/
  yuJingTianShu: [{required: true, message: '请输入预警天数', trigger: 'blur'}]
};

const onSubmit = async () => {
    formRef.value.validate().then(async (res) => {
      try {
        await useSaveUpdateChuanYuanZhengShuApi(formState.value)
        useMessage().success("操作成功")
        handleClose()
        emits("success")
      } catch (e) {
        useMessage().error(e.msg)
      }
    })
};

const handleOpen = async (record) => {
  open.value = true
  if (record && record.id) {
    const resp = await useGetChuanYuanZhengShuApi(record.id)
    formState.value = resp.data
  }
}

const handleClose = () => {
  open.value = false
  formRef.value.clearValidate()
  formRef.value.resetFields()
  formState.value = {
    id: "",
    xiangMuDaiMa: '',
    xiangMuMing: '',
    qianFaJiGuan: '',
    qianFaRiQi: '',
    youXiaoQi: '',
    zhuangTai: 1
  }
}

defineExpose({
  handleOpen,
})

</script>

<style lang="scss" scoped>
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }

  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
    overflow: auto;
    border-radius: 0px;
  }

  :where(.css-dev-only-do-not-override-1hsjdkk).ant-modal .ant-modal-header {
    background: transparent;
  }

  .ant-modal-body {
    flex: 1;
  }
}
</style>
